﻿using LightCAD.Core;
using System.Reflection;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace LightCAD.Runtime
{
    public class CommandCenter
    {

        public static CommandCenter Instance = new CommandCenter();
        private CommandCenter() { }

        public  event CommandExecuteEventHandler CommandExecute;
 

        public  DictionaryArray<LcCommand> Commands { get; }= new DictionaryArray<LcCommand>();

        public LcCommand FindCommand(string command)
        {
            if (Commands != null)
            {
                foreach (LcCommand cmd in Commands)
                {
                    if (command.ToLower().Equals(cmd.MainCommand.ToLower()))
                    {
                        return cmd;
                    }
                    foreach (string str in cmd.ShortCuts)
                    {
                        if (command.ToLower().Equals(str.ToLower()))
                        {
                            return cmd;
                        }
                    }
                }
            }
            return null;
        }

        public MethodInfo FindCommandMethod(LcCommand command)
        {
            if (command == null)
            {
                return null;
            }
            System.Type type = System.Type.GetType(command.Type);
            if (type == null)
            {
                Assembly assembly = null;
                try
                {
                    foreach (Assembly assembly2 in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        if (assembly2.ManifestModule.Name == command.DllName)
                        {
                            assembly = assembly2;
                        }
                    }
                    if (assembly == null)
                    {
                        string dllName = command.DllName;
                        //this.Document.FindFile(com.DllName, out dllName);
                        //assembly = Assembly.Load(AssemblyName.GetAssemblyName(dllName));
                    }
                }
                catch
                {
                    goto Label_0128;
                }
                if (assembly == null)
                {
                    goto Label_0128;
                }
                type = assembly.GetType(command.Type);
                if (type == null)
                {
                    goto Label_0128;
                }
            }
            return type.GetMethod(command.FunctionName);
        Label_0128:
            return null;
        }

        public LcCommand ParseCommand(string fullCommandLine)
        {
            if ((fullCommandLine != "") && !fullCommandLine.StartsWith("//"))
            {
                string[] strArray = fullCommandLine.Split(new char[] { ',' });
                LcCommand com = new LcCommand();
                int num = 1;
                foreach (string str in strArray)
                {
                    string str2 = str.Trim();
                    switch (num)
                    {
                        case 1:
                            com.MainCommand = str2;
                            break;

                        case 2:
                            com.ShortCuts.AddItem(str2);
                            break;

                        case 3:
                            com.DllName = str2;
                            break;

                        case 4:
                            com.Type = str2;
                            break;

                        case 5:
                            com.FunctionName = str2;
                            break;
                    }
                    num++;
                }
               return com;
            }
            return null;
        }


        public bool OnCommandExecute(object? sender,string commandname, bool isDefaultImplemented)
        {
            bool success = false;
            if (this.CommandExecute != null)
            {
                var eventArgs = new CommandExecuteEventArgs
                {
                    CommandName = commandname,
                    IsDefaultImplemented = isDefaultImplemented
                };
                this.CommandExecute(this, eventArgs);
                success = eventArgs.Success ?? false;
            }
            return success;
        }
    }
}